package com.whale.cloud.util.solr;

import com.alibaba.fastjson.JSON;
import com.whale.cloud.util.TimeUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.json.JSONObject;

import java.util.*;
/** Created by maoweidong on 2017/7/13. */
@Log4j2
public class NewSolrRead {

  private static CloudSolrClient cloudSolrClient = null;
  private static NewSolrRead newSolrRead = null;
  private static Collection<String> zkhost_collection;
  private static final String zkHost =
      "m1:2181,s1:2181,s2:2181,s3:2181,s4:2181,s5:2181,s6:2181,s7:2181,"
          + "s8:2181,s9:2181,s10:2181,s11:2181,s12:2181,s13:2181,s14:2181,s15:2181,s16:2181,s17:2181,s18:2181,s19:2181,s20:2181,s21:2181,s22:2181,s23:2181,s24:2181";
  private static final String defaultCollection = "collection_vip";
  private static final int zkClientTimeout = 20000;
  private static final int zkConnectTimeout = 1000;

  public NewSolrRead() {
    zkhost_collection = new ArrayList<>();
    String[] zkHosts = zkHost.split(",");
    for (int i = 0; i < zkHosts.length; i++) {
      zkhost_collection.add(zkHosts[i]);
    }
    //连接集群
    if (cloudSolrClient == null) {
      try {
        cloudSolrClient = new CloudSolrClient(zkhost_collection, "/solr");
        cloudSolrClient.setDefaultCollection(defaultCollection);
        cloudSolrClient.setZkClientTimeout(zkClientTimeout);
        cloudSolrClient.setZkConnectTimeout(zkConnectTimeout);
        cloudSolrClient.connect();
      } catch (Exception e) {
        System.out.println("The URL of zkHost is not correct!");
        e.printStackTrace();
      }
    }
  }

  public static CloudSolrClient NewSolrRead(String collection) {
    CloudSolrClient cloudSolrClient = null;
    zkhost_collection = new ArrayList<>();
    String[] zkHosts = zkHost.split(",");
    for (int i = 0; i < zkHosts.length; i++) {
      zkhost_collection.add(zkHosts[i]);
    }
    //连接集群
    if (cloudSolrClient == null) {
      try {
        cloudSolrClient = new CloudSolrClient(zkhost_collection, "/solr");
        cloudSolrClient.setDefaultCollection(collection);
        cloudSolrClient.setZkClientTimeout(zkClientTimeout);
        cloudSolrClient.setZkConnectTimeout(zkConnectTimeout);
        cloudSolrClient.connect();
      } catch (Exception e) {
        System.out.println("The URL of zkHost is not correct!");
        e.printStackTrace();
      }
    }
    return cloudSolrClient;
  }

  //实例化类
  public static NewSolrRead getInstance() {

    newSolrRead = new NewSolrRead();

    return newSolrRead;
  }

  /**
   * {"CARD_TYPE_ID" : "\xE6\xBD\x8D\xE5\x9D\x8A\xE8\xB4\xB5 \xE5\xAE\xBE\xE5\x8D\xA1", "OPEN_ID" :
   * "~!@#$%&*", "SEX_VIP" : "W", "T_VB_Y" : "1959", "T_DL_D" : "31", "PHONE_VIP" : "~!@#$%&*",
   * "CARD_NO_VIP" : "JZ13775598699A8", "T_DL_Y " : "2099", "T_CR_D" : "09", "MOBILE_VIP" :
   * "13775598699", "T_CR_Y" : "2014", "NAME_VI P" : "\xE7\x8E\x8B\xE4\xBF\xAE\xE8\x90\x8D",
   * "T_CR_M" : "06", "T_VB_D" : "20", "T_CR_W " : "24", "T_VB_M" : "02", "T_DL_M" : "12",
   * "FR_ACTIVE" : "N", "EMP_ID" : "29239"} IS_SHAREBIR
   */
  public static String switchkey(String key) {

    int valuekey = Integer.parseInt(key);

    switch (valuekey) {
      case 1:
        key = "BIRTHDAY"; //生日
        break;
      case 2:
        key = "ANNIVERSARY"; //纪念日
        break;
      case 3:
        key = "NUM_TRADE"; //消费次数
        break;
      case 4:
        key = "AMT_TRADE"; //消费金额
        break;
      case 5:
        key = "POINTS"; //积分
        break;
      case 6:
        key = "FR_ACTIVE";
        break;
      case 7:
        key = "NAME_VIP"; //姓名
        break;
      case 8:
        key = "SEX_VIP"; //性别
        break;
      case 9:
        key = "AGE"; //年龄
        break;
      case 10:
        key = "MOBILE_VIP"; //手机号
        break;
      case 11:
        key = "CARD_NO_VIP"; //卡号
        break;
      case 12:
        key = "CARD_TYPE_ID";
        break;
      case 13:
        key = "JOIN_DATE"; //开卡日期
        break;
      case 14:
        key = "STORE_ID"; //店铺id
        break;
      case 15:
        key = "EMP_ID"; //导购
        break;
      case 16:
        key = "VIP_GROUP_CODE"; //会员分组编号
        break;
      case 18:
        key = "OPEN_ID";
        break;
      case 20:
        key = "JOIN_DATE_NUM";
        break;
    }

    return key;
  }

  public String querySolr(
      List<HashMap<String, Object>> singleList,
      List<String> datelist,
      String groups,
      List<String> sort,
      int pagenumber,
      int pagesize) {

    HashMap<String, Object> signmap = new HashMap<String, Object>();
    StringBuilder sb = new StringBuilder();
    //  List<String> list = new ArrayList<String>();
    HashMap<String, Object> Jsonmap = new HashMap<String, Object>();
    String json = null;

    try {
      if (singleList != null && datelist != null) {

        //拼接索引字段
        for (int i = 0; i < singleList.size(); i++) {
          signmap = singleList.get(i);
          sb.append("(");
          for (String character : signmap.keySet()) {
            HashMap<String, Object> map = (HashMap<String, Object>) signmap.get(character);
            for (String key : map.keySet()) {
              sb.append(key)
                  .append(":")
                  .append(map.get(key).toString())
                  .append(" " + character + " ");
            }
            sb.delete(sb.lastIndexOf(character), sb.length());
          }
          sb.append(")");
        }
        //第二参数为传入的日期集合
        if (datelist != null) {
          sb.append(" AND ");
          for (int j = 0; j < datelist.size(); j++) {
            sb.append(datelist.get(j)).append(" AND ");
          }
          sb.delete(sb.length() - 5, sb.length());
        }

        //会员分组
        if (groups != null) {
          sb.append(" AND ").append(groups);
        }

        System.out.println("stringbuild的字段：  " + sb.toString());

        SolrQuery solrQuery = new SolrQuery(sb.toString());
        //排序
        if (sort != null && sort.size() > 0) {
          solrQuery.addSort(sort.get(0), SolrQuery.ORDER.desc);
          solrQuery.addSort(sort.get(1), SolrQuery.ORDER.desc);
          solrQuery.addSort(sort.get(2), SolrQuery.ORDER.desc);
        }

        //分页查询
        solrQuery.setStart((pagenumber - 1) * pagesize);
        solrQuery.setRows(pagesize);

        QueryResponse resp = cloudSolrClient.query(solrQuery, SolrRequest.METHOD.POST);
        SolrDocumentList hits = resp.getResults();

        //获取rowkey集合 已排序
        //  list = getrowkeys(hits);
        Jsonmap.put("status", "查询成功");
        Jsonmap.put("documents", hits);
        Jsonmap.put("size", hits.getNumFound());
      } else if (singleList == null && datelist != null) {

        //拼接索引字段
        //第二参数为传入的日期集合
        if (datelist != null) {
          //  sb.append(" AND ");
          for (int j = 0; j < datelist.size(); j++) {
            sb.append(datelist.get(j)).append(" AND ");
          }
          sb.delete(sb.length() - 5, sb.length());
        }

        //会员分组
        if (groups != null) {
          sb.append(" AND ").append(groups);
        }
        System.out.println("stringbuild的字段：  " + sb.toString());

        SolrQuery solrQuery = new SolrQuery(sb.toString());
        //排序
        if (sort != null && sort.size() > 0) {
          solrQuery.addSort(sort.get(0), SolrQuery.ORDER.desc);
          solrQuery.addSort(sort.get(1), SolrQuery.ORDER.desc);
          solrQuery.addSort(sort.get(2), SolrQuery.ORDER.desc);
        }

        //分页查询
        solrQuery.setStart((pagenumber - 1) * pagesize);
        solrQuery.setRows(pagesize);
        QueryResponse resp = cloudSolrClient.query(solrQuery, SolrRequest.METHOD.POST);
        SolrDocumentList hits = resp.getResults();
        //获取rowkey集合 已排序
        //  list = getrowkeys(hits);
        Jsonmap.put("status", "查询成功");
        Jsonmap.put("documents", hits);
        Jsonmap.put("size", hits.getNumFound());
      } else if (singleList != null && datelist == null) {
        //拼接索引字段
        for (int i = 0; i < singleList.size(); i++) {
          signmap = singleList.get(i);
          sb.append("(");
          for (String character : signmap.keySet()) {
            HashMap<String, Object> map = (HashMap<String, Object>) signmap.get(character);
            for (String key : map.keySet()) {
              sb.append(key)
                  .append(":")
                  .append(map.get(key).toString())
                  .append(" " + character + " ");
            }
            sb.delete(sb.lastIndexOf(character), sb.length());
          }
          sb.append(")");
        }

        //会员分组
        if (groups != null) {
          sb.append(" AND ").append(groups);
        }
        //第二参数为传入的日期集合
        System.out.println("stringbuild的字段：  " + sb.toString());

        SolrQuery solrQuery = new SolrQuery(sb.toString());
        //排序
        if (sort != null && sort.size() > 0) {
          solrQuery.addSort(sort.get(0), SolrQuery.ORDER.desc);
          solrQuery.addSort(sort.get(1), SolrQuery.ORDER.desc);
          solrQuery.addSort(sort.get(2), SolrQuery.ORDER.desc);
        }

        //分页查询
        solrQuery.setStart((pagenumber - 1) * pagesize);
        solrQuery.setRows(pagesize);
        QueryResponse resp = cloudSolrClient.query(solrQuery, SolrRequest.METHOD.POST);
        SolrDocumentList hits = resp.getResults();
        //获取rowkey集合 已排序
        //  list = getrowkeys(hits);
        Jsonmap.put("status", "查询成功");
        Jsonmap.put("documents", hits);
        Jsonmap.put("size", hits.getNumFound());
      } else if (singleList == null && datelist == null && groups != null) {

        sb.append(groups);

        //第二参数为传入的日期集合
        System.out.println("stringbuild的字段：  " + sb.toString());

        SolrQuery solrQuery = new SolrQuery(sb.toString());
        //排序
        if (sort != null && sort.size() > 0) {
          solrQuery.addSort(sort.get(0), SolrQuery.ORDER.desc);
          solrQuery.addSort(sort.get(1), SolrQuery.ORDER.desc);
          solrQuery.addSort(sort.get(2), SolrQuery.ORDER.desc);
        }

        //分页查询
        solrQuery.setStart((pagenumber - 1) * pagesize);
        solrQuery.setRows(pagesize);
        QueryResponse resp = cloudSolrClient.query(solrQuery, SolrRequest.METHOD.POST);
        SolrDocumentList hits = resp.getResults();
        //获取rowkey集合 已排序
        //  list = getrowkeys(hits);
        Jsonmap.put("status", "查询成功");
        Jsonmap.put("documents", hits);
        Jsonmap.put("size", hits.getNumFound());

      } else {
        SolrQuery solrQuery = new SolrQuery("*:*");
        if (sort != null && sort.size() > 0) {
          //排序
          solrQuery.addSort(sort.get(0), SolrQuery.ORDER.desc);
          solrQuery.addSort(sort.get(1), SolrQuery.ORDER.desc);
          solrQuery.addSort(sort.get(2), SolrQuery.ORDER.desc);
        }

        //分页查询
        solrQuery.setStart((pagenumber - 1) * pagesize);
        solrQuery.setRows(pagesize);
        QueryResponse resp = cloudSolrClient.query(solrQuery, SolrRequest.METHOD.POST);
        SolrDocumentList hits = resp.getResults();
        //获取rowkey集合 已排序
        //  list = getrowkeys(hits);
        Jsonmap.put("status", "查询成功");
        Jsonmap.put("documents", hits);
        Jsonmap.put("size", hits.getNumFound());
      }
    } catch (Exception e) {
      Jsonmap.put("status", "查询失败");
      Jsonmap.put("e", e.getMessage());
      e.printStackTrace();
    }
    JSONObject jsonObject = new JSONObject(Jsonmap);
    json = jsonObject.toString();

    return json;
  }

  //获取某个字段的集合
  //    public static List<String> getrowkeys(SolrDocumentList documentList) {
  //
  //        List<String> list = new ArrayList<String>();
  //
  //        try {
  //            if (documentList.size() > 0) {
  //                for (SolrDocument document : documentList) {
  //
  //                    String rowkey = document.getFieldValue("rowkey").toString();
  //                    list.add(rowkey);
  //
  //                }
  //            }
  //        } catch (Exception e) {
  //            e.printStackTrace();
  //        }
  //        return list;
  //    }

  //将日期条件转换为字符串添加到list
  public static ArrayList<String> getStringdate(
      ArrayList<ArrayList<HashMap<String, Object>>> Dlist) {

    HashMap<String, Object> signmap = new HashMap<String, Object>();
    ArrayList<HashMap<String, Object>> list = new ArrayList<>();
    ArrayList<String> listsb = new ArrayList<>();
    if (Dlist != null) {
      //拼接索引字段
      for (int i = 0; i < Dlist.size(); i++) {
        StringBuilder sb = new StringBuilder();
        sb.append("(");
        list = Dlist.get(i);
        for (int j = 0; j < list.size(); j++) {
          signmap = list.get(j);
          sb.append("(");
          for (String character : signmap.keySet()) {
            HashMap<String, Object> map = (HashMap<String, Object>) signmap.get(character);
            for (String key : map.keySet()) {
              sb.append(key)
                  .append(":")
                  .append(map.get(key).toString())
                  .append(" " + character + " ");
            }
            sb.delete(sb.lastIndexOf(character), sb.length());
          }
          sb.append(")");
        }
        sb.append(")");
        listsb.add(sb.toString());
      }
    }
    return listsb;
  }

  public static String switchtoString(String values) {
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("(");
    String[] infos = values.split(",");
    for (int f = 0; f < infos.length; f++) {
      stringBuilder.append('"').append(infos[f]).append('"').append(",");
    }
    stringBuilder.deleteCharAt(stringBuilder.length() - 1);
    stringBuilder.append(")");

    return stringBuilder.toString();
  }

  //分组查询拼接字段

  public static String switchGroup(List<HashMap<String, Object>> signlist, List<String> datelist) {

    HashMap<String, Object> signmap = new HashMap<String, Object>();
    StringBuilder sb = new StringBuilder();

    if (signlist != null && datelist != null) {

      //拼接索引字段
      for (int i = 0; i < signlist.size(); i++) {
        signmap = signlist.get(i);
        sb.append("(");
        for (String character : signmap.keySet()) {
          HashMap<String, Object> map = (HashMap<String, Object>) signmap.get(character);
          for (String key : map.keySet()) {
            sb.append(key)
                .append(":")
                .append(map.get(key).toString())
                .append(" " + character + " ");
          }
          sb.delete(sb.lastIndexOf(character), sb.length());
        }
        sb.append(")");
      }
      //第二参数为传入的日期集合
      if (datelist != null) {
        sb.append(" AND ");
        for (int j = 0; j < datelist.size(); j++) {
          sb.append(datelist.get(j)).append(" AND ");
        }
        sb.delete(sb.length() - 5, sb.length());
      }

      System.out.println("sb的字段：  " + sb.toString());

    } else if (signlist == null && datelist != null) {

      //拼接索引字段
      //第二参数为传入的日期集合
      if (datelist != null) {
        //  sb.append(" AND ");
        for (int j = 0; j < datelist.size(); j++) {
          sb.append(datelist.get(j)).append(" AND ");
        }
        sb.delete(sb.length() - 5, sb.length());
      }

      System.out.println("sb的字段：  " + sb.toString());

    } else if (signlist != null && datelist == null) {
      //拼接索引字段
      for (int i = 0; i < signlist.size(); i++) {
        signmap = signlist.get(i);
        sb.append("(");
        for (String character : signmap.keySet()) {
          HashMap<String, Object> map = (HashMap<String, Object>) signmap.get(character);
          for (String key : map.keySet()) {
            sb.append(key)
                .append(":")
                .append(map.get(key).toString())
                .append(" " + character + " ");
          }
          sb.delete(sb.lastIndexOf(character), sb.length());
        }
        sb.append(")");
      }
      System.out.println("sb的字段：  " + sb.toString());
    }

    return sb.toString();
  }

  public void InsertViptoSolr(
      String corp_code,
      String open_id,
      String vipId,
      String app_id,
      String vip_card_no,
      String vip_phone)
      throws Exception {
    com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject(true);
    jsonObject.put("CORP_ID", corp_code);
    jsonObject.put("VIP_ID", vipId);

    String time =
        TimeUtils.convertTime(TimeUtils.getCurrentTimeInString(TimeUtils.DATE_FORMAT_DATE_NO));
    String[] times = time.split(",");

    SolrInputDocument solrInputFields = new SolrInputDocument();

    solrInputFields.addField("id", jsonObject.toJSONString());
    solrInputFields.addField("CORP_ID", corp_code);
    solrInputFields.addField("VIP_ID", vipId);
    solrInputFields.addField("MOBILE_VIP", vip_phone);
    solrInputFields.addField("PHONE_VIP", vip_phone);
    solrInputFields.addField("CARD_NO_VIP", vip_card_no);
    solrInputFields.addField("OPEN_ID", open_id);
    solrInputFields.addField("APP_ID", app_id);
    solrInputFields.addField("T_CR_Y", times[0]);
    solrInputFields.addField("T_CR_M", times[1]);
    solrInputFields.addField("T_CR_W", times[2]);
    solrInputFields.addField("T_CR_D", times[3]);
    solrInputFields.addField("FR_ACTIVE", "Y");
    solrInputFields.addField("EMP_ID", "~!@#$%&*");
    solrInputFields.addField("STORE_ID", "~!@#$%&*");
    cloudSolrClient.add(solrInputFields);
    cloudSolrClient.commit();
  }

  /** 该方法singleList datelist groups 三者关系为AND，单传使，另外两个参数可为NULL singleList中所有的HashMap关系为AND */
  public String querySolr(
      String groups, HashMap<String, Object> sort, int pagenumber, int pagesize) {
    StringBuilder sb = new StringBuilder();
    sb.append("NOT FR_ACTIVE:N").append(" AND ");
    HashMap<String, Object> Jsonmap = new HashMap<String, Object>();
    String json = null;
    try {
      sb.append(groups);
      log.info("===Solr===" + sb.toString());
      SolrQuery solrQuery = new SolrQuery(sb.toString());
      //排序
      if (sort != null && sort.size() > 0) {
        Set set = sort.keySet();
        if (set.contains("desc")) {
          List<String> sortList = (List<String>) sort.get("desc");
          for (int i = 0; i < sortList.size(); i++) {
            solrQuery.addSort(sortList.get(i), SolrQuery.ORDER.desc);
          }
        } else if (set.contains("asc")) {
          List<String> sortList = (List<String>) sort.get("asc");
          for (int i = 0; i < sortList.size(); i++) {
            solrQuery.addSort(sortList.get(i), SolrQuery.ORDER.asc);
          }
        }
      }
      //分页查询
      solrQuery.setStart((pagenumber - 1) * pagesize);
      solrQuery.setRows(pagesize);
      QueryResponse resp = cloudSolrClient.query(solrQuery, SolrRequest.METHOD.POST);
      SolrDocumentList hits = resp.getResults();
      Jsonmap.put("status", "查询成功");
      Jsonmap.put("documents", hits);
      Jsonmap.put("size", hits.getNumFound());
    } catch (Exception e) {
      Jsonmap.put("status", "查询失败");
      Jsonmap.put("e", e.getMessage());
      e.printStackTrace();
    }
    com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject(Jsonmap);
    json = jsonObject.toString();
    return json;
  }

  public String querySolrNew(
      String groups, HashMap<String, Object> sort, String[] fl, int pagenumber, int pagesize) {
    StringBuilder sb = new StringBuilder();
    sb.append("NOT FR_ACTIVE:N").append(" AND ");
    HashMap<String, Object> Jsonmap = new HashMap<String, Object>();
    String json = null;
    try {
      sb.append(groups);
      log.info("===Solr===" + sb.toString());
      SolrQuery solrQuery = new SolrQuery(sb.toString());
      //排序
      if (sort != null && sort.size() > 0) {
        Set set = sort.keySet();
        if (set.contains("desc")) {
          List<String> sortList = (List<String>) sort.get("desc");
          for (int i = 0; i < sortList.size(); i++) {
            solrQuery.addSort(sortList.get(i), SolrQuery.ORDER.desc);
          }
        } else if (set.contains("asc")) {
          List<String> sortList = (List<String>) sort.get("asc");
          for (int i = 0; i < sortList.size(); i++) {
            solrQuery.addSort(sortList.get(i), SolrQuery.ORDER.asc);
          }
        }
      }
      //分页查询
      solrQuery.setStart((pagenumber - 1) * pagesize);
      solrQuery.setRows(pagesize);
      if (null != fl) {
        if (fl.length > 0) {
          solrQuery.set("fl", fl);
        }
      }
      QueryResponse resp = cloudSolrClient.query(solrQuery, SolrRequest.METHOD.POST);
      SolrDocumentList hits = resp.getResults();
      Jsonmap.put("status", "查询成功");
      Jsonmap.put("documents", hits);
      Jsonmap.put("size", hits.getNumFound());
    } catch (Exception e) {
      Jsonmap.put("status", "查询失败");
      Jsonmap.put("e", e.getMessage());
      e.printStackTrace();
    }
    com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject(Jsonmap);
    json = jsonObject.toString();
    return json;
  }

  /** 查询所有未分配会员 */
  public static void updateBySolr(com.alibaba.fastjson.JSONArray jsonArray) {
    CloudSolrClient cloudSolrClient = NewSolrRead("collection_vip");
    try {
      com.alibaba.fastjson.JSONArray jsonArray2 = new com.alibaba.fastjson.JSONArray();
      for (int i = 0; i < jsonArray.size(); i++) {
        com.alibaba.fastjson.JSONObject object = jsonArray.getJSONObject(i);
        String VIP_ID = object.getString("VIP_ID");
        String CORP_ID = object.getString("CORP_ID");
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder
            .append("VIP_ID:")
            .append(VIP_ID)
            .append(" AND ")
            .append("CORP_ID:")
            .append(CORP_ID);
        String result =
            newSolrRead.querySolr(stringBuilder.toString(), (HashMap<String, Object>) null, 1, 10);
        com.alibaba.fastjson.JSONObject resultObj = JSON.parseObject(result);
        String documents = resultObj.getString("documents");
        com.alibaba.fastjson.JSONArray jsonArray1 = JSON.parseArray(documents);
        for (int j = 0; j < jsonArray1.size(); j++) {
          com.alibaba.fastjson.JSONObject object1 = jsonArray1.getJSONObject(j);
          object1.putAll(object);
          jsonArray2.add(object1);
        }
      }
      for (int i = 0; i < jsonArray2.size(); i++) {
        SolrInputDocument solrInputDocument = new SolrInputDocument();
        com.alibaba.fastjson.JSONObject jsonObject = jsonArray2.getJSONObject(i);
        for (String key : jsonObject.keySet()) {
          solrInputDocument.addField(key, jsonObject.get(key));
        }
        cloudSolrClient.add(solrInputDocument);
      }
      cloudSolrClient.commit();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static String switchCustomParam(
      List<HashMap<String, Object>> singleList, List<String> datelist, String groups) {
    HashMap<String, Object> signmap = new HashMap<String, Object>();
    StringBuilder sb = new StringBuilder();
    if (singleList != null && datelist != null) {
      //拼接索引字段
      for (int i = 0; i < singleList.size(); i++) {
        signmap = singleList.get(i);
        sb.append("(");
        for (String character : signmap.keySet()) {
          HashMap<String, Object> map = (HashMap<String, Object>) signmap.get(character);
          for (String key : map.keySet()) {
            sb.append(key)
                .append(":")
                .append(map.get(key).toString())
                .append(" " + character + " ");
          }
          sb.delete(sb.lastIndexOf(character), sb.length());
        }
        sb.append(")");
      }
      //第二参数为传入的日期集合
      if (datelist != null) {
        sb.append(" AND ");
        for (int j = 0; j < datelist.size(); j++) {
          sb.append(datelist.get(j)).append(" AND ");
        }
        sb.delete(sb.length() - 5, sb.length());
      }

      //会员分组
      if (groups != null) {
        sb.append(" AND ").append(groups);
      }

      //  System.out.println("stringbuild的字段：  " + sb.toString());

    } else if (singleList == null && datelist != null) {

      //拼接索引字段
      //第二参数为传入的日期集合
      if (datelist != null) {
        //  sb.append(" AND ");
        for (int j = 0; j < datelist.size(); j++) {
          sb.append(datelist.get(j)).append(" AND ");
        }
        sb.delete(sb.length() - 5, sb.length());
      }

      //会员分组
      if (groups != null) {
        sb.append(" AND ").append(groups);
      }
      //   System.out.println("stringbuild的字段：  " + sb.toString());

    } else if (singleList != null && datelist == null) {
      //拼接索引字段
      for (int i = 0; i < singleList.size(); i++) {
        signmap = singleList.get(i);
        sb.append("(");
        for (String character : signmap.keySet()) {
          HashMap<String, Object> map = (HashMap<String, Object>) signmap.get(character);
          for (String key : map.keySet()) {
            sb.append(key)
                .append(":")
                .append(map.get(key).toString())
                .append(" " + character + " ");
          }
          sb.delete(sb.lastIndexOf(character), sb.length());
        }
        sb.append(")");
      }

      //会员分组
      if (groups != null) {
        sb.append(" AND ").append(groups);
      }
      //第二参数为传入的日期集合
      //   System.out.println("stringbuild的字段：  " + sb.toString());

    } else if (singleList == null && datelist == null && groups != null) {

      sb.append(groups);

      //第二参数为传入的日期集合
      //    System.out.println("stringbuild的字段：  " + sb.toString());
    }
    return sb.toString();
  }

  public static void InsertBySolr(com.alibaba.fastjson.JSONArray jsonArray) {
    CloudSolrClient cloudSolrClient = NewSolrRead("collection_vip");
    try {
      for (int i = 0; i < jsonArray.size(); i++) {
        SolrInputDocument solrInputDocument = new SolrInputDocument();
        com.alibaba.fastjson.JSONObject jsonObject = jsonArray.getJSONObject(i);
        for (String key : jsonObject.keySet()) {
          solrInputDocument.addField(key, jsonObject.get(key));
        }
        cloudSolrClient.add(solrInputDocument);
      }
      cloudSolrClient.commit();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
